

import os
from datetime import datetime, timedelta, timezone

prod_file_name = "prod0704.txt"     # prod.txt
camera_file_name = "camera0704.txt"   # cameras.txt


def str_to_datetime(time_str:str):
    return datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S.%f")

list1 = list()
f1=open(prod_file_name, encoding='utf-8')
pre = "a"
while True and pre:
    line = f1.readline()
    line = line.replace("\n", "")
    if line:
        line_parts = line.split("$")
        camera_code_cmd = line_parts[1]
        camera_code_cmd_parts = camera_code_cmd.split(",")
        code = camera_code_cmd_parts[3]
        list1.append(dict(time=f'{line_parts[0]}', code=code))
    pre = line
f1.close()
# print(f"-----list1: {list1} ")


list2 = list()
f2=open(camera_file_name, encoding='utf-8')
pre = "a"
while True and pre:
    line = f2.readline()
    line = line.replace("\n", "")
    if line:
        line_parts = line.split("$")
        list2.append(dict(time=f"{line_parts[0]}", code=f"{line_parts[1]}"))
    pre = line
f2.close()
# print(f"-----list2:  {list2} ")


diff_items = []
def diff_code():
    for item in list1:
        aa = [i for i in list2 if i["code"] == item["code"]]
        if len(aa) == 0:
            diff_items.append(item)
diff_code()
print(f"-----size:  {len(diff_items)} ")
print(f"-----diff_items:  {diff_items} ")


for camera_item in list2:
    items_prod = [item for item in list1 if item["code"] == camera_item["code"]]
    if len(items_prod) > 0:
        prod_item = items_prod[0]
        code = camera_item["code"]
        gap = (str_to_datetime(camera_item["time"])- str_to_datetime(prod_item["time"])).seconds
        if gap > 1:
            print(f"prod_time: {prod_item['time']}, camera_time:{camera_item['time']} , code:{code} , gap:{gap}")